<?php 

	/*
		Voodoo Validator
		www.voodoovalidator.com
	*/
	
	/*
		For help: admin@voodoovalidator.com
	*/
	
	/*
		This application was developed originally by 
		Robbie Vasquez - www.robbievasquez.com
		Casey McLaughlin - www.caseymclaughlin.com
	*/
	
	/*
		BIRTHDAY: June 1st, 2009
		LAST MODIFIED: 08/12/2010
		BY: Robbie Vasquez
	*/

//================================================

	/*
		This script gets the results of the W3 validation
		process. jQuery is needed.
		
		This script validates a single web page/css file
		(refreshes it from previous validation process).
		
		The address of the file it validates
		comes from the $_GET variabled 'url' and
		runs it through the W3 validator, yielding xml
		results. The xml is analyzed for errors and 
		warnings.
		
		The $_GET variable 'file' is
		for displaying the name of the validated file
		in the output.
		
		The $_GET variables 'url' and 'file' are provided
		by the script 'validate_w3c_v4.php'
		
		The xml output provides the number of errors
		and the number of warnings. The script displays
		the validation condition of the file as pulled
		from that xml.
		
		The result is printed on the voodoovalidator.php web page
		in place of the previous validation results.
	*/

//================================================

	// Quit trying after this set time
	set_time_limit(90);


	// These variables come from the javascript
	// in validate_w3c_v4.php
	// The $_GET variable 'file' is not needed, just
	// for display purposes.
	$the_url = $_GET['url'];
	$the_file = $_GET['file'];


	// To view the output results on this file alone to analyze what it is doing
	if (isset($_GET['dumpdata']))
		$dump_data = TRUE;


	// Chosen only these file types are allowed to run through the validator
	$allowed_extensions = array('php', 'html', 'htm', 'asp', 'aspx', 'css', 'shtml', 'phtml');
	
	
	// Separate the file from its extension.
	// File names with a '.' in the name may not operate correctly
	$url_sections = explode('.', $the_url);
	$file_extension = strtolower(trim(array_pop($url_sections)));
	
	
	// If the type of the file is not accepted, indicate
	if ( ! in_array($file_extension, $allowed_extensions))
	{
		echo '<span class="na">' . $the_file . ' cannot be validated.</span>';
		exit();
	}
	
	
	// The validator can do both css and web page files, but not with the same validator.
	
	if($file_extension != 'css')
		$xml_data = file_get_contents('http://validator.w3.org/check?uri=' . $the_url . '&output=soap12&charset=utf-8');
		
	if ($file_extension == 'css')
		$xml_data = file_get_contents('http://jigsaw.w3.org/css-validator/validator?uri=' . $the_url . '&output=soap12&charset=utf-8');
	
	if (isset($dump_data))
	{
		header("Content-type: text/plain");
		echo $xml_data;
		exit();
	}
	
	// An array is needed to hold the number of errors
	$error_matches = array();
	
	// <m:errorcount></m:errorcount> is a piece in the xml output of the W3 validator
	preg_match("/<m\:errorcount>(.*)<\/m\:errorcount>/", $xml_data, $error_matches);
	$warning_matches = array();
	
	// <m:warnings></m:warnings> is a piece in the xml output of the W3 validator
	preg_match("/<m\:warningcount>(.*)<\/m\:warningcount>/", $xml_data, $warning_matches);

	if (isset($error_matches[1]))
		$number_of_errors = $error_matches[1];
	else
		$number_of_errors = 0;
		
	if (isset($warning_matches[1]))
		$number_of_warnings = $warning_matches[1];
	else
		$number_of_warnings = 0;

	
	if ($number_of_errors > 0)
	{
		// If there are errors, this script plans to print the following
		
		if($file_extension != 'css')
			$html_out = '<div class="errors">' . $the_file . ' has Errors: ' . $number_of_errors . ' - <a href="http://validator.w3.org/check?uri=' . $the_url . '">See the errors</a></div>';
			
		if($file_extension == 'css')
			$html_out = '<div class="errors">' . $the_file . ' has CSS Errors: ' . $number_of_errors . '</div>';
	}
		
	if ($number_of_errors == 0)
	{
		// If there are no errors, this script plans to print the following
		// Since this is only refreshing, it does not need to be within a div
		// of class 'refresh' as it does in validate_w3c_v4.php
		
		if($file_extension != 'css')
			$html_out = '<div class="valid">' . $the_file . ' is Valid! <span class="warnings">There are ' . $number_of_wardnings . ' warnings.</span> - <a href="http://validator.w3.org/check?uri=' . $the_url . '">See the results</a></div>';
		
		if($file_extension == 'css')
			$html_out = '<div class="valid">' . $the_file . ' is Valid CSS! <span class="warnings">There are ' . $number_of_wardnings . ' warnings.</span></div>';
	}
	
	// Print the results onto voodoovalidator.php
	echo $html_out;
	
	unset($the_url);
	unset($the_file);

/* EOF */